<?php

namespace Xiaoc\Api;
/**
 * Xiaoc会员订单
 */
class Order extends Common
{
    private $user_id;
    private $user_name;

    public function getRules()
    {
        $data["*"] = array(
            'user_token' => array('name' => 'user_token', 'require' => true, 'desc' => '用户token')
        );
        $data['user_try_order'] = array(
            'page' => array('name' => 'page', 'desc' => '页数', 'require' => true, 'default' => 1, 'type' => 'int'),
            'status' => array('name' => 'status', 'desc' => '状态0申请中1成功2失败', 'type' => 'int'),
        );
        $data['user_order_list'] = array(
            'status' => array('name' => 'status', 'desc' => '1待付款2待收货3待发货4已完成', 'default' => 0, 'type' => 'int'),
            'page' => array('name' => 'page', 'desc' => '页数', 'require' => true, 'default' => 1, 'type' => 'int'),
            'extension_code' => array('name' => 'extension_code', 'desc' => 'group_by众筹try_goods试用')
        );
        $data['uesr_del_order'] = array(
            'order_id' => array('name' => 'order_id', 'desc' => '订单ID', 'require' => true, 'type' => 'int')
        );
        $data['see_invoice'] = array(
            'order_id' => array('name' => 'order_id', 'desc' => '订单ID', 'require' => true, 'type' => 'int')
        );
        $data['user_order_info'] = array(
            'order_id' => array('name' => 'order_id', 'desc' => '订单ID', 'require' => true, 'type' => 'int')
        );
        $data['user_cancel_order'] = array(
            'order_id' => array('name' => 'order_id', 'desc' => '订单ID', 'require' => true, 'type' => 'int')
        );
        $data['user_order_comment'] = array(
            'order_id' => array('name' => 'order_id', 'desc' => '订单ID', 'require' => true, 'type' => 'int'),
            'content' => array('name' => 'content', 'desc' => '内容', 'require' => true),
            'goods_id' => array('name' => 'goods_id', 'desc' => '商品ID', 'require' => true, 'type' => 'int'),
            'rec_id' => array('name' => 'rec_id', 'desc' => 'rec_id', 'require' => true, 'type' => 'int'),
            'rec_id' => array('name' => 'comment_rank', 'desc' => '评分', 'require' => true, 'type' => 'int', 'default' => 5),
        );
        $data['user_order_refund_apply'] = array(
            'order_id' => array('name' => 'order_id', 'desc' => '订单ID', 'require' => true, 'type' => 'int'),
            'rec_id' => array('name' => 'rec_id', 'desc' => '订单下商品的rec_id', 'require' => true, 'type' => 'int'),
        );
        $data['user_order_refund_submit'] = array(
            'cause_id' => array('name' => 'cause_id', 'desc' => '原因ID', 'require' => true, 'type' => 'int'),
            'num' => array('name' => 'num', 'desc' => '退款数量', 'require' => true, 'type' => 'int'),
            'content' => array('name' => 'content', 'desc' => '退款说明', 'require' => true),
            'order_id' => array('name' => 'order_id', 'desc' => '订单ID', 'require' => true, 'type' => 'int'),
            'rec_id' => array('name' => 'rec_id', 'desc' => '订单下商品的rec_id', 'require' => true, 'type' => 'int'),
            'remark' => array('name' => 'remark', 'desc' => '会员留言', 'require' => true),
        );
        $data['user_order_refund_cancel'] = array(
            'ret_id' => array('name' => 'ret_id', 'desc' => '记录ID', 'require' => true, 'type' => 'int'),
        );
        $data['user_order_refund_list'] = array(
            'page' => array('name' => 'page', 'desc' => '页数', 'require' => true, 'default' => 1, 'type' => 'int'),
        );
        $data['order_affirm_received'] = array(
            'order_id' => array('name' => 'order_id', 'desc' => '订单ID', 'require' => true, 'type' => 'int'),
        );
        return $data;
    }

    /**
     * 请忽略
     * @desc 该接口已作废
     */
    public function userCheck()
    {
        $user_ifno = $this->check_login($this->user_token);
        $this->user_id = $user_ifno['user_id'];
        $this->user_name = $user_ifno['user_name'];
    }

    /**
     * 会员试用
     * @desc 当status为1时且order_id为真时可以查看订单详情
     * @return string goods_name 商品名称
     * @return float goods_price 商品价格
     * @return string goods_thumb 商品图片
     * @return int order_id 订单id
     * @return string addtime 申请时间
     * @return string status 状态0申请中1成功2失败
     */
    public function user_try_order()
    {
        $status = intval($this->status);
        $join['[>]goods'] = array('goods_id');
        $where['try_apply.user_id'] = $this->user_id;
        if (in_array($status, array(0, 1, 2))) {
            $where['try_apply.status'] = $status;
        } else {
            $where['try_apply.status[!]'] = -1;
        }
        $where['LIMIT'] = array($this->limitStart, $this->limitEnd);
        $where['ORDER'] = array('try_apply.apply_id' => 'DESC');
        $field = array('goods.goods_name', 'goods.market_price(goods_price)', 'goods.goods_thumb', 'try_apply.order_id', 'try_apply.addtime', 'try_apply.status');
        $list = $this->medoo->select('try_apply', $join, $field, $where);
        $count = 0;
        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]['addtime'] = time_format($value['addtime']);
            }
            $count = $this->medoo->count('try_apply', $join, 'try_apply.apply_id', $where);
        } else {
            $list = array();
        }
        $data['list'] = $list;
        $data['hasmore'] = $this->HasMore($count);
        return $data;
    }

    /**
     * 会员订单列表
     * @desc 会员订单列表
     * @return string order_sn 订单号
     * @return array order_status 订单状态(status=1取消2确认收货3评论)(return=1申请售后)(is_del==1删除)(is_pay==1支付)
     * @return string add_time 订单生成时间
     * @return int order_id 订单id
     * @return string extension_code 等于group_by众筹try_goods试用
     * @return array order_goods 该订单下的商品(goods_thumb商品图片,goods_name商品名称,goods_number购买数量,goods_price商品价格,goods_id商品ID)
     * @return float order_amount 订单总金额
     * @return string invoice_no 当为真是可以查看物流
     */
    public function user_order_list()
    {
        $user_id = $this->user_id;
        $status = $this->status;//1待付款2待收货3待发货4已完成
        $limit = $this->limitStart . ',' . $this->limitEnd;
        $extension_code = $this->extension_code;
        $where = 'a.user_id = ' . $user_id . ' AND a.is_delete = 0';
        $prefix = get_table_prefix();
        $pay_id = $this->payment_id_list();
        if (in_array($status, array(1, 2, 3, 4))) {
            if ($status == 1) {
                $where .= ' AND a.pay_status in (0,3) AND a.order_status in (1,5) AND ( a.shipping_status in (1,2) OR a.pay_id ' . db_create_in($this->payment_id_list(false)) . ')';
            } elseif ($status == 2) {
                $where .= ' AND a.order_status in (1,5,6) AND a.shipping_status = 1 AND a.pay_status in (2,1) ';
            } elseif ($status == 3) {
                $where .= ' AND a.order_status in (1,5,6) AND a.shipping_status in (0,3,5) AND ( a.pay_status in (1,2) OR a.pay_id ' . db_create_in($pay_id) . ')';
            } elseif ($status == 4) {
                $where .= ' AND a.order_status in (1,5) AND a.shipping_status = 2 AND a.pay_status in (1,2) ';
            }
        }
        if ($extension_code) {
            $where .= ' AND a.extension_code=' . trim($extension_code);
        }
        $select = ' (SELECT count(*) FROM ' . $prefix . 'comment' . ' AS c WHERE c.comment_type = 0 AND c.id_value = b.goods_id AND c.rec_id = b.rec_id AND c.parent_id = 0 AND c.user_id = \'' . $user_id . '\') AS sign1 ';
        $sql = 'SELECT a.order_sn, a.order_status, a.pay_id, a.order_id, a.add_time, a.order_amount, a.shipping_status, a.extension_code, a.pay_time,a.invoice_no,a.pay_status, ' . $select . ' FROM ' . $prefix . 'order_info' . ' as a left join ' . $prefix . 'order_goods' . ' as b on a.order_id = b.order_id ' . ' left join ' . $prefix . 'goods' . ' as c on b.goods_id = c.goods_id where ' . $where . ' AND (select count(*) from ' . $prefix . 'order_info' . ' as d where d.main_order_id = a.order_id) = 0' . ' AND a.is_zc_order = 0 ' . ' group by a.order_id ORDER BY a.add_time DESC Limit ' . $limit;
        $list = $this->notorm->order_info->queryAll($sql);
        $count = 0;
        if ($list) {
            $sign = $this->medoo->get('shop_config', 'value', array('code' => 'sign'));
            foreach ($list as $key => $value) {
                $list[$key]['order_goods'] = $this->get_order_goods($value['order_id']);
                $list[$key]['order_status'] = $this->get_order_status($value, $sign);
                $list[$key]['add_time'] = time_format($value['add_time']);
                unset($list[$key]['pay_time']);
                unset($list[$key]['pay_status']);
                unset($list[$key]['shipping_status']);
                unset($list[$key]['sign1']);
            }
            $sql2 = 'SELECT  count(a.order_id) AS num ' . ' FROM ' . $prefix . 'order_info  as a left join ' . $prefix . 'order_goods' . ' as b on a.order_id = b.order_id ' . ' left join ' . $prefix . 'goods  as c on b.goods_id = c.goods_id where ' . $where . ' AND (select count(*) from ' . $prefix . 'order_info as d where d.main_order_id = a.order_id) = 0' . ' AND a.is_zc_order = 0 ' . ' group by a.order_id';
            $count = count($this->notorm->order_info->queryAll($sql2));
        } else {
            $list = array();
        }
        $data['lsit'] = $list;
        $data['hasmore'] = $this->HasMore($count);
        return $data;
    }

    /**
     * 会员删除订单
     * @desc 会员删除订单
     * @return ret 200成功
     */
    public function uesr_del_order()
    {
        $where['user_id'] = $this->user_id;
        $where['order_id'] = intval($this->order_id);
        $is_del = 0;
        if ($where['order_id'] > 0) {
            $order = $this->medoo->get('order_info', array('order_status', 'pay_status', 'shipping_status'), $where['order_id']);
            if ($order) {
                if (($order['order_status'] == 2) || (($order['order_status'] == 5) && ($order['shipping_status'] == 2) && ($order['pay_status'] == 2))) {
                    $is_del = 1;
                }
                if ($is_del == 1) {
                    $up['is_delete'] = 1;
                    $result = $this->medoo->update('order_info', $up, $where);
                    if ($result) {
                        $this->api_success_msg('删除成功');
                    } else {
                        $this->api_error_msg('删除失败');
                    }
                }
            }
        }
        $this->api_error_msg('该订单不存在或不能删除');
    }

    /**
     * 会员订单详情
     * @desc 会员订单详情
     * @return string order_sn 订单号
     * @return array order_status 订单状态(status=1取消2确认收货3评论)(return=1申请售后)(is_del==1删除)(is_pay==1支付)
     * @return string add_time 订单生成时间
     * @return int order_id 订单id
     * @return string extension_code 等于group_by众筹try_goods试用
     * @return array order_goods 该订单下的商品(goods_thumb商品图片,goods_name商品名称,goods_number购买数量,goods_price商品价格,goods_id商品ID)
     * @return float order_amount 订单总金额
     * @return string invoice_no 当为真是可以查看物流
     * @return string pay_time 支付时间
     * @return string confirm_time 确认时间
     * @return string address 收货地址
     * @return string mobile 手机号
     * @return string name 收货人姓名
     * @return float user_money 使用余额金额
     * @return string postscript 用户留言
     * @return string inv_type 发票类型
     * @return string inv_payee 发票抬头
     * @return string inv_content 发票内容
     */
    public function user_order_info()
    {
        $order_info = array();
        if (intval($this->order_id)) {
            $prefix = get_table_prefix();
            $user_id = $this->user_id;
            $order_id = $this->order_id;
            $where = ' a.user_id = ' . $user_id . ' AND  a.order_id= ' . $order_id;
            $select = ' (SELECT count(*) FROM ' . $prefix . 'comment' . ' AS c WHERE c.comment_type = 0 AND c.id_value = b.goods_id AND c.rec_id = b.rec_id AND c.parent_id = 0 AND c.user_id = \'' . $user_id . '\') AS sign1 ';
            $sql = 'SELECT a.order_sn, a.order_status, a.order_id, a.add_time, a.order_amount, a.shipping_status, a.extension_code, a.pay_time,a.invoice_no,a.pay_status,a.province,a.city,a.district,a.consignee AS name,a.surplus AS user_money,a.inv_payee,a.pay_name,a.shipping_fee,a.shipping_time,a.mobile,a.address,a.confirm_time,a.inv_content,a.inv_type,a.pay_id, ' . $select . ' FROM ' . $prefix . 'order_info' . ' as a left join ' . $prefix . 'order_goods' . ' as b on a.order_id = b.order_id ' . ' left join ' . $prefix . 'goods' . ' as c on b.goods_id = c.goods_id where ' . $where . ' AND (select count(*) from ' . $prefix . 'order_info' . ' as d where d.main_order_id = a.order_id) = 0' . ' AND a.is_zc_order = 0 ' . ' group by a.order_id ORDER BY a.add_time DESC Limit 1';
            $order_info = $this->notorm->order_info->queryAll($sql);
            if (isset($order_info[0])) {
                $order_info = $order_info[0];
                $sign = $this->medoo->get('shop_config', 'value', array('code' => 'sign'));
                $order_info['order_goods'] = $this->get_order_goods($order_info['order_id']);
                $order_info['order_status'] = $this->get_order_status($order_info, $sign);
                $order_info['add_time'] = time_format($order_info['add_time']);
                if (0 < $order_info['pay_time'] && $order_info['pay_status'] != PS_UNPAYED) {
                    $order_info['pay_time'] = time_format($order_info['pay_time']);
                } else {
                    $order_info['pay_time'] = '';
                }
                if ((0 < $order_info['shipping_time']) && in_array($order_info['shipping_status'], array(SS_SHIPPED, SS_RECEIVED))) {
                    $order_info['shipping_time'] = time_format($order_info['shipping_time']);
                } else {
                    $order_info['shipping_time'] = '';
                }
                if ((0 < $order_info['confirm_time']) && (($order_info['order_status'] == OS_CONFIRMED) || ($order_info['order_status'] == OS_SPLITED) || ($order_info['order_status'] == OS_SPLITING_PART))) {
                    $order['confirm_time'] = time_format($order_info['confirm_time']);
                } else {
                    $order['confirm_time'] = '';
                }
                //收货地址
                $order_info['address'] = $this->get_address($order_info['province'], $order_info['city'], $order_info['district']) . $order_info['address'];
                unset($order_info['province']);
                unset($order_info['city']);
                unset($order_info['district']);
                unset($order_info['sign1']);
                //发票
                if ($order_info['inv_type']) {
                    $res = $this->medoo->get('shop_config', 'value', array('code' => $order_info['inv_type']));
                    $order_info['inv_type'] = $res;
                }
                unset($order_info['pay_status']);
                unset($order_info['shipping_status']);
            } else {
                $order_info = array();
            }
        }
        return $data['info'] = $order_info;
    }

    /**
     * 查看物流
     * @desc 查看物流
     * @return string time 时间
     * @return int state ：2在途中,3签收,4问题件 ；
     * @return array datas time 时间 context描述
     * @return  string invoice_no 物流单号
     * @return  string shipping_name 物流名称
     */
    public function see_invoice()
    {
        $where['order_id'] = intval($this->order_id);
        $where['user_id'] = $this->user_id;
        if ($where['order_id'] > 0) {
            $order_info = $this->medoo->get('order_info', array('shipping_id', 'invoice_no'), $where);
            if ($order_info['shipping_id'] && $order_info['invoice_no']) {
                $shipping_code = $this->medoo->get('shipping', array('shipping_code', 'shipping_name'), array('shipping_id' => $order_info['shipping_id']));
                $obj = new \Express_Lite();
                $res = $obj->line($shipping_code['shipping_code'], $order_info['invoice_no']);
                if ($res['result']) {
                    $list['datas'] = array_order($res['data'], 'time', 'desc');
                    $list['state'] = $res['state'];
                } else {
                    $this->api_error_msg($res['msg']);
                }
                $list['invoice_no'] = $order_info['invoice_no'];
                $list['invoice_name'] = $shipping_code['shipping_name'];
                return $list;
            }
        }
        return array();
    }

    /**
     * 取消订单
     * @desc 取消订单
     * @return int ret 200 成功
     */
    public function user_cancel_order()
    {
        $order_id = intval($this->order_id);
        $user_id = $this->user_id;
        if ($order_id > 0) {
            $where['order_id'] = $order_id;
            $where['user_id'] = $user_id;
            $field = array('order_sn', 'surplus', 'integral', 'bonus_id', 'pay_status', 'order_status', 'shipping_status', 'main_order_id');
            $order_info = $this->medoo->get('order_info', $field, $where);
            if ($order_info) {
                if ($order_info['shipping_status'] != 0) {
                    $this->api_error_msg('只有在未发货状态下才能取消');
                }
                if ($order_info['pay_status'] != 0) {
                    $this->api_error_msg('只有未付款状态才能取消');
                }
                //改变订单状态
                $res = $this->medoo->update('order_info', array('order_status' => 2), $where);
                if ($res) {
                    //添加订单操作日志
                    $add_log['order_id'] = $order_id;
                    $add_log['action_user'] = $this->user_name;
                    $add_log['order_status'] = 2;
                    $add_log['action_note'] = '用户取消';
                    $add_log['shipping_status'] = $order_info['shipping_status'];
                    $add_log['pay_status'] = $order_info['pay_status'];
                    $add_log['log_time'] = gmtime();
                    $this->medoo->insert('order_action', $add_log);
                    $log_id = $this->medoo->id();
                    if ($log_id) {
                        //退回预付款
                        if ($order_info['surplus'] > 0) {
                            $desc = '取消订单 ' . $order_info['order_sn'] . '退回支付订单时使用的预付款';
                            $user_info = $this->medoo->get('users', array('user_money'), array('user_id' => $user_id));
                            $user_money = $order_info['surplus'];
                            //修改用户账户资金
                            $user_res = $this->medoo->update('users', array('user_money' => $user_info['user_money'] + $user_money), array('user_id' => $user_id));
                            if ($user_res) {
                                //添加记录
                                $account_log['user_id'] = $user_id;
                                $account_log['user_money'] = $user_money;
                                $account_log['frozen_money'] = 0;
                                $account_log['change_time'] = gmtime();
                                $account_log['change_desc'] = $desc;
                                $account_log['change_type'] = 99;
                                $this->medoo->insert('account_log', $account_log);
                                $account_log_id = $this->medoo->id();
                                if ($account_log_id) {
                                    //改变订单金额
                                    $res_order = $this->medoo->update('order_info', array('surplus' => 0), $where);
                                    if (!$res_order) {
                                        $this->api_error_msg('预存款错误');
                                    }
                                }
                            } else {
                                $this->api_error_msg('账户余额出现异常');
                            }
                        }
                        //改变商品库存
                        $config_where['code'] = array('use_storage', 'stock_dec_time');
                        $configs = $this->medoo->select('shop_config', array('value', 'code'), $config_where);
                        $stock_dec_time = 0;
                        $use_storage = 0;
                        foreach ($configs as $value) {
                            if ($value['code'] == 'use_storage') {
                                $use_storage = intval($value['value']);
                            }
                            if ($value['code'] == 'stock_dec_time') {
                                $stock_dec_time = intval($value['value']);
                            }
                        }
                        if ($stock_dec_time == 1 && $use_storage == 1) {
                            $res_goods = $this->change_order_goods_storage($order_id);
                            if ($res_goods) {
                                $this->api_success_msg('取消成功');
                            } else {
                                $this->api_error_msg('库存错误');
                            }
                        } else {
                            $this->api_success_msg('取消成功');
                        }
                    } else {
                        $this->api_error_msg('订单日志错误');
                    }
                } else {
                    $this->api_error_msg('订单状态修改失败');
                }
            } else {
                $this->api_error_msg('该订单不存在');
            }
        }
    }

    /**
     * 订单评价
     * @desc 订单评价
     * @return int ret 200 成功
     */
    public function user_order_comment()
    {
        $order_id = $this->order_id;
        $content = $this->content;
        $rec_id = $this->rec_id;
        $goods_id = $this->goods_id;
        $comment_rank = $this->comment_rank;
        $rank_server = 5;
        $rank_delivery = 5;
        $addtime = gmtime();
        $ip = getIp();
        $ru_id = $this->medoo->get('goods', 'ru_id', array('goods_id' => $goods_id));
        $configs = $this->medoo->get('shop_config', 'value', array('code' => 'comment_check'));
        $status = 1 - $configs;
        $other = array('comment_type' => 0, 'id_value' => $goods_id, 'email' => '', 'user_name' => $this->user_name, 'content' => $content, 'comment_rank' => $comment_rank, 'comment_server' => $rank_server, 'comment_delivery' => $rank_delivery, 'add_time' => $addtime, 'ip_address' => $ip, 'status' => $status, 'parent_id' => 0, 'user_id' => $this->user_id, 'single_id' => 0, 'order_id' => $order_id, 'rec_id' => $rec_id, 'goods_tag' => '', 'ru_id' => $ru_id);
        $this->medoo->insert('comment', $other);
        if ($this->medoo->id()) {
            $this->api_success_msg('评价成功');
        }
        $this->api_error_msg('评价失败');
    }

    /**
     * 退款申请
     * @desc 退款申请
     * @return array cause_list 退款原因 cause_name名称  cause_id ID
     * @return array goods_info 商品信息 goods_name商品名称 goods_number购买数量 goods_attr规格 goods_price商品价格
     */
    public function user_order_refund_apply()
    {
        $user_id = $this->user_id;
        $order_id = intval($this->order_id);
        $rec_id = intval($this->rec_id);
        if ($order_id > 0) {
            $where['user_id'] = $user_id;
            $where['order_id'] = $order_id;
            $field = array('order_sn', 'surplus', 'order_amount', 'order_status', 'shipping_status', 'pay_status', 'pay_time');
            $order_info = $this->medoo->get('order_info', $field, $where);
            if ($order_info) {
                $sign = $this->medoo->get('shop_config', 'value', array('code' => 'sign'));
                $order_status = $this->get_order_status($order_info, $sign);
                if ($order_status['return'] == 1) {
                    $order_return = $this->medoo->get('order_return', array('rec_id'), array('rec_id' => $rec_id, 'order_id' => $order_id));
                    if ($order_return) {
                        $this->api_error_msg('同一订单的同一商品不能重复提交');
                    }
                    //获取退款的原因
                    $cause_where['parent_id'] = 0;
                    $cause_where['is_show'] = 1;
                    $cause_where['ORDER'] = array('sort_order' => 'DESC');
                    $data['cause_list'] = $this->medoo->select('return_cause', array('cause_name', 'cause_id'), $cause_where);
                    //商品信息
                    $data['goods_info'] = $this->get_order_goods_info($rec_id);
                    $data['goods_info']['rec_id'] = $rec_id;
                    return $data;
                } else {
                    $this->api_error_msg('该订单暂不支持退款');
                }
            }
        }
        $this->api_error_msg('暂无数据');
    }

    /**
     * 退款申请提交
     * @desc 退款申请提交
     * @return int ret 200 成功
     */
    public function user_order_refund_submit()
    {
        $cause_id = $this->cause_id;
        $order_id = $this->order_id;
        $user_id = $this->user_id;
        $refund_type = 1;//退货
        $num = intval($this->num);
        $content = $this->content;
        $rec_id = intval($this->rec_id);
        $remark = $this->remark;
        if ($num <= 0) $num = 1;
        $order_return = $this->medoo->get('order_return', 'rec_id', array('rec_id' => $rec_id, 'order_id' => $order_id));
        if ($order_return) {
            $this->api_error_msg('同一订单的同一商品不能重复提交');
        }
        $prefix = get_table_prefix();
        $sql = 'select g.goods_name, g.goods_sn,g.brand_id, og.order_id, og.goods_id, og.product_id, og.goods_attr, og.warehouse_id, og.area_id, o.order_sn, ' . ' og.is_real, og.goods_attr_id, og.goods_price, og.goods_number, o.user_id ' . 'from ' . $prefix . 'order_goods' . ' as og ' . ' left join ' . $prefix . 'goods' . ' as g on og.goods_id = g.goods_id ' . ' left join ' . $prefix . 'order_info' . ' as o on o.order_id = og.order_id ' . ' where og.rec_id = \'' . $rec_id . '\'';
        $order_goods = $this->notorm->order_goods->queryAll($sql);
        if (!isset($order_goods[0])) {
            $this->api_error_msg('退换货提交出现异常，请稍后重试');
        }
        $order_goods = $order_goods[0];
        if ($order_goods['user_id'] != $user_id) {
            $this->api_error_msg('退换货提交出现异常，请稍后重试');
        }
        if ($num > $order_goods['goods_number']) {
            $this->api_error_msg('退款数量不能大于购买的数量');
        }
        $order_info = $this->medoo->get('order_info', array('country', 'consignee', 'province', 'city', 'district', 'address', 'zipcode', 'mobile'), array('order_id' => $order_goods['order_id'], 'user_id' => $user_id));
        if ($order_info) {
            $add_return['rec_id'] = $rec_id;
            $add_return['goods_id'] = $order_goods['goods_id'];
            $add_return['order_id'] = $order_goods['order_id'];
            $add_return['order_sn'] = $order_goods['order_sn'];
            $add_return['return_type'] = $refund_type;
            $add_return['maintain'] = 0;
            $add_return['back'] = 1;
            $add_return['exchange'] = 0;
            $add_return['user_id'] = $user_id;
            $add_return['goods_attr'] = $order_goods['goods_attr'];
            $add_return['apply_time'] = gmtime();
            $add_return['actual_return'] = '';
            $add_return['return_status'] = 0;
            $add_return['attr_val'] = '';
            $add_return['cause_id'] = $cause_id;
            $add_return['country'] = $order_info['country'];
            $add_return['province'] = $order_info['province'];
            $add_return['city'] = $order_info['city'];
            $add_return['district'] = $order_info['district'];
            $add_return['phone'] = $order_info['mobile'];
            $add_return['address'] = $order_info['address'];
            $add_return['addressee'] = $order_info['consignee'];
            $add_return['return_sn'] = get_order_sn();
            $add_return['return_brief'] = $content;
            $add_return['remark'] = $remark;
            $add_return['credentials'] = 0;
            $add_return['should_return'] = $order_goods['goods_price'] * $num;
            $this->medoo->insert('order_return', $add_return);
            $return_id = $this->medoo->id();
            if ($return_id) {
                $return_action['ret_id'] = $return_id;
                $return_action['action_user'] = '买家';
                $return_action['action_note'] = $remark;
                $this->medoo->insert('return_action', $return_action);
                if ($this->medoo->id()) {
                    //获取品牌名称
                    $brand_name = $this->medoo->get('brand', 'brand_name', array('brand_id' => $order_goods['brand_id']));
                    $return_goods['brand_name'] = '';
                    if ($brand_name) $return_goods['brand_name'] = $brand_name;
                    $return_goods['rec_id'] = $rec_id;
                    $return_goods['ret_id'] = $return_id;
                    $return_goods['goods_id'] = $order_goods['goods_id'];
                    $return_goods['goods_name'] = $order_goods['goods_name'];
                    $return_goods['product_id'] = $order_goods['product_id'];
                    $return_goods['goods_sn'] = $order_goods['goods_sn'];
                    $return_goods['is_real'] = $order_goods['is_real'];
                    $return_goods['goods_attr'] = $order_goods['goods_attr'];
                    $return_goods['attr_id'] = $order_goods['goods_attr_id'];
                    $return_goods['refound'] = $order_goods['goods_price'];
                    $return_goods['return_type'] = $refund_type;
                    $return_goods['return_number'] = $num;
                    $return_goods['out_attr'] = '';
                    $this->medoo->insert('return_goods', $return_goods);
                    if ($this->medoo->id()) {
                        $order_return_extend['ret_id'] = $return_id;
                        $order_return_extend['return_number'] = $num;
                        $this->medoo->insert('order_return_extend', $order_return_extend);
                        $this->api_success_msg('提交成功，工作人员将尽快审核！');
                    }
                }
            }
        }
        $this->api_error_msg('提交失败');
    }

    /**
     * 会员退款记录
     * @desc 退款申请提交
     * @return int ret_id 申请ID
     * @return int goods_id 商品ID
     * @return string order_sn 订单号
     * @return string apply_time 申请时间
     * @return float should_return 退款金额
     * @return string return_sn 退款单号
     * @return int return_number 退款数量
     * @return int is_cancel ==1可以取消
     * @return int status ==1已退款0审核中
     * @return string goods_thumb 商品缩略图
     * @return string goods_name 商品名称
     */
    public function user_order_refund_list()
    {
        $user_id = $this->user_id;
        $prefix = get_table_prefix();
        $limit = $this->limitStart . ',' . $this->limitEnd;
        $goodsSql = '(SELECT CONCAT(goods_thumb, \',\', goods_name) FROM ' . $prefix . 'goods' . ' WHERE goods_id = o.goods_id)';
        $actSql = '(SELECT CONCAT(activity_thumb, \',\', act_name)  FROM ' . $prefix . 'goods_activity' . ' WHERE act_id = o.goods_id) ';
        $sql = 'SELECT o.ret_id , o.goods_id , o.order_sn, o.apply_time , o.should_return, o.return_status , o.refound_status, o.return_sn, rg.return_number, IF(og.extension_code = \'package_buy\', ' . $actSql . ', ' . $goodsSql . ') AS goods_info ' . ' FROM ' . $prefix . 'order_return' . ' AS o' . ' LEFT JOIN ' . $prefix . 'order_goods' . ' AS og ON og.rec_id = o.rec_id' . ' LEFT JOIN ' . $prefix . 'return_goods' . ' AS rg ON rg.ret_id = o.ret_id' . ' WHERE o.user_id = \'' . $user_id . '\'  order by o.ret_id DESC Limit ' . $limit;
        $data = $this->notorm->order_return->queryAll($sql);
        $count = 0;
        if ($data) {
            foreach ($data as $key => $value) {
                $value['is_cancel'] = 0;
                $value['status'] = 0;
                $goodsInfo = explode(',', $value['goods_info']);
                $value['goods_thumb'] = $goodsInfo[0];
                $value['goods_name'] = $goodsInfo[1];
                $value['apply_time'] = time_format($value['apply_time']);
                if (($value['return_status'] == 0) && ($value['refound_status'] == 0)) {
                    $value['is_cancel'] = 1;
                }
                if ($value['refound_status'] == 1 && $value['return_status'] == 1) {
                    $value['status'] = 1;
                }
                unset($value['return_status']);
                unset($value['refound_status']);
                unset($value['goods_info']);
                $list[] = $value;
            }
            $sql2 = 'SELECT o.ret_id , o.goods_id , o.order_sn, o.apply_time , o.should_return, o.return_status , o.refound_status, o.return_sn, rg.return_number, IF(og.extension_code = \'package_buy\', ' . $actSql . ', ' . $goodsSql . ') AS goods_info ' . ' FROM ' . $prefix . 'order_return' . ' AS o' . ' LEFT JOIN ' . $prefix . 'order_goods' . ' AS og ON og.rec_id = o.rec_id' . ' LEFT JOIN ' . $prefix . 'return_goods' . ' AS rg ON rg.ret_id = o.ret_id' . ' WHERE o.user_id = \'' . $user_id . '\'  order by o.ret_id DESC ';
            $count = count($this->notorm->order_return->queryAll($sql2));

        } else {
            $list = array();
        }
        $arr['list'] = $list;
        $arr['hasmore'] = $this->HasMore($count);
        return $arr;
    }

    /**
     * 会员退款取消
     * @desc 会员退款取消
     * @return ret 200 成功
     */
    public function user_order_refund_cancel()
    {
        $ret_id = intval($this->ret_id);
        $user_id = $this->user_id;
        if ($ret_id > 0) {
            $where['ret_id'] = $ret_id;
            $where['user_id'] = $user_id;
            $return_info = $this->medoo->get('order_return', array('return_status', 'refound_status'), $where);
            if ($return_info) {
                $prefix = get_table_prefix();
                if ($return_info['return_status'] == 0 && $return_info['refound_status'] == 0) {
                    $sql = 'delete from ' . $prefix . 'order_return_extend' . ' where ret_id = \'' . $ret_id . '\' ';
                    $data = $this->notorm->order_return->queryAll($sql);
                    if ($data) {
                        $return_action['ret_id'] = $ret_id;
                        $return_action['action_user'] = '买家';
                        $return_action['action_note'] = '取消';
                        $return_action['return_status'] = 5;
                        $this->medoo->insert('return_action', $return_action);
                        if ($this->medoo->id()) {
                            $this->api_success_msg('取消成功');
                        }
                    }
                    $this->api_error_msg('取消失败');
                } else {
                    $this->api_error_msg('当前退款记录不能取消');
                }
            }
        }
        $this->api_error_msg('该记录不存在');
    }

    /**
     * 会员确认收货
     * @desc 会员确认收货
     * @return ret 200 成功
     */
    public function order_affirm_received()
    {
        $order_id = $this->order_id;
        $where['user_id'] = $this->user_id;
        $where['order_id'] = $order_id;
        $order = $this->medoo->get('order_info', array('order_sn', 'order_status', 'shipping_status', 'pay_status'), $where);
        if ($order) {
            if ($order['shipping_status'] == 3) {
                $this->api_error_msg('此订单已经确认过了，感谢您的惠顾，欢迎再次光临');
            }
            if ($order['shipping_status'] != 1) {
                $this->api_error_msg('您提交的订单不正确');
            }
            $res = $this->medoo->update('order_info', array('shipping_status' => 2), $where);
            if ($res) {
                //添加订单日志
                $add_log['order_id'] = $order_id;
                $add_log['action_user'] = $this->user_name;
                $add_log['order_status'] = $order['order_status'];
                $add_log['shipping_status'] = 2;
                $add_log['pay_status'] = $order['pay_status'];
                $add_log['action_note'] = '用户确认收货';
                $add_log['log_time'] = gmtime();
                $this->medoo->insert('order_action', $add_log);
                $log_id = $this->medoo->id();
                if ($log_id) {
                    $this->api_success_msg('确认成功');
                }
            }
        }
        $this->api_success_msg('确认失败');
    }
}